<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/9
 * Time: 11:18
 */
namespace app\admin\controller;
use think\captcha\Captcha;
use think\Controller;
use think\Request;

class Login extends Controller {

    /**
     * 登录页
     */
    public function index(){
        if (request()->isPost()){
            $verify = new Captcha ();
            if (!$verify->check(input('param.verify'))) {
                return json(['code'=>0,'msg'=>'验证码错误']);
            }
            $data['admin_username'] = input('param.username','');
            $data['admin_pwd'] = input('param.password','');
            $is_online = input('param.online',0);
            $validate = validate('Admin');
            if (!$validate->check($data)){
                return json(['code'=>0,'msg'=>$validate->getError()]);
            }else{
                $admin= model('Admin');
                if($admin->login($data['admin_username'], $data['admin_pwd'], $is_online)){
                    return json(['code'=>200,'msg'=>'恭喜您，登陆成功']);
                }else{
                    return json(['code'=>0,'msg'=>$admin->getError()]);
                }
            }
        }else{
            if($this->check_admin_login()) $this->redirect('admin/Index/index');
            return $this->fetch();
        }
    }

    /**
     * 验证码
     */
    public function captcha_src(){

        $captcha = new Captcha();
        return $captcha->entry();
    }

    /**
     * 判断是否已登录
     */

    protected function check_admin_login(){
        return model('admin/Admin')->is_login();
    }

    /**
     * 退出
     */
    public function login_out(){
        session('admin_auth',null);
        session('admin_auth_sign',null);
        cookie('aid', null);
        cookie('signin_token', null);
        $this->redirect('login/index');
    }

    /**
     * 空操作
     */
    public function _empty(){
        $this->redirect('admin/login');
    }
}